package com.sdonkey.score.controller;

import com.sdonkey.score.bean.FriendBean;
import com.sdonkey.score.bean.UserAccountBean;
import com.sdonkey.score.bean.UserLoginBean;
import com.sdonkey.score.dto.UserAccountVo;
import com.sdonkey.score.exception.CustomerException;
import com.sdonkey.score.log.Log;
import com.sdonkey.score.service.IAdScoreManagerService;
import com.sdonkey.score.service.IUserAccountService;
import com.sdonkey.score.service.IUserLoginService;
import com.sdonkey.score.service.IUserMsgService;
import com.sdonkey.score.util.FormValidateUtil;
import com.sdonkey.score.util.MD5;
import com.sdonkey.score.util.ScoreHelper;
import com.sdonkey.score.util.ValidateCodeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 账户中心
 *
 * @author 赵超
 * @email 773134958@qq.com
 */
@Controller
@RequestMapping(value = "/account", method = RequestMethod.POST)
public class UserAccountController {

    private Logger logger = Logger.getLogger(UserAccountController.class);
    @Autowired
    IUserAccountService accountCentralService;
    @Autowired
    IAdScoreManagerService adScoreManagerService;
    @Autowired
    IUserLoginService loginService;
    @Autowired
    IUserMsgService userMsgService;

    // ---------------------------基本信息------------------------------------

    /**
     * 用户信息
     */
    @ResponseBody
    @RequestMapping("/ajaxbasicInfo")
    public UserLoginBean basicInfoajax(HttpSession session) throws Exception {
        UserLoginBean userLoginBean = accountCentralService
                .queryUserInformation(Long.parseLong(session.getAttribute("id").toString()));
        return userLoginBean;
    }

    /**
     * 更新用户基本信息
     */
    @ResponseBody
    @RequestMapping("/ajaxUpdateBasic")
    public String ajaxUpdateBasic(HttpSession session, UserLoginBean userLoginBean) throws Exception {
        String status;
        Integer devId = (Integer) session.getAttribute("id");
        userLoginBean.setDevId(devId);
        int a = accountCentralService.update_BasicInformation(userLoginBean);
        if (a > 0) {
            status = "1"; // 更新成功
            session.setAttribute("username", userLoginBean.getName());
            logger.info("用户" + devId + " 更新 基本信息");
        } else {
            status = "-1"; // 更新失败
        }
        return status;
    }

    /**
     * 更新用户详细信息(包括性别 年龄)
     */
    @ResponseBody
    @RequestMapping("/ajaxUpdateDetails")
    public String ajaxUpdateDetails(HttpSession session, UserLoginBean userLoginBean) throws Exception {
        String status;
        Integer devId = (Integer) session.getAttribute("id");
        userLoginBean.setDevId(devId);
        int a = accountCentralService.update_Details(userLoginBean);
        if (a > 0) {
            status = "1"; // 更新成功
            session.setAttribute("username", userLoginBean.getName());
            logger.info("用户" + devId + " 更新 详细信息");
        } else {
            status = "-1"; // 更新失败
        }
        return status;
    }

    /**
     * 更新用户 广告类型
     */
    @ResponseBody
    @RequestMapping("/ajaxUpdateCategory")
    public String ajaxUpdateCategory(HttpSession session, UserLoginBean userLoginBean) throws Exception {
        String status;
        Integer devId = (Integer) session.getAttribute("id");
        userLoginBean.setDevId(devId);
        int a = accountCentralService.update_Ad_category(userLoginBean);
        if (a > 0) {
            status = "1"; // 更新成功
            session.setAttribute("username", userLoginBean.getName());
            logger.info("用户" + devId + " 更新 广告类型");
        } else {
            status = "-1"; // 更新失败
        }
        return status;
    }

    /**
     * 更新广告需求关键字
     */
    @ResponseBody
    @RequestMapping("/ajaxUpdateAd_key")
    public String ajaxUpdateAd_key(HttpSession session, UserLoginBean userLoginBean) throws Exception {
        String status;
        Integer devId = (Integer) session.getAttribute("id");
        userLoginBean.setDevId(devId);
        int a = accountCentralService.update_Ad_Key(userLoginBean);
        if (a > 0) {
            status = "1"; // 更新成功
            session.setAttribute("username", userLoginBean.getName());
            logger.info("用户" + devId + " 更新 广告需求关键字");
        } else {
            status = "-1"; // 更新失败
        }
        return status;
    }

    /**
     * 更新用户 兴趣爱好
     */
    @ResponseBody
    @RequestMapping("/ajaxUpdateHobby")
    public String ajaxUpdateHobby(HttpSession session, UserLoginBean userLoginBean) throws Exception {
        String status;
        Integer devId = (Integer) session.getAttribute("id");
        userLoginBean.setDevId(devId);
        int a = accountCentralService.update_User_Hobby(userLoginBean);
        if (a > 0) {
            status = "1"; // 更新成功
            session.setAttribute("username", userLoginBean.getName());
            logger.info("用户" + devId + " 更新 兴趣爱好");
        } else {
            status = "-1"; // 更新失败
        }
        return status;
    }

    /**
     * 新用户绑定邮箱获取验证码
     */
    @ResponseBody
    @RequestMapping("/ajaxGetCodeByNewMail")
    public String sendCodebyNewMail(String mail, String code, HttpSession session) throws Exception {
        String status;
        UserLoginBean user = (UserLoginBean) session.getAttribute("user");
        if (FormValidateUtil.isEmpty(user.getMail())) {//新用户，没有绑定邮箱的用户
            boolean flag = accountCentralService.queryishaveUserByMail(mail);
            if (!flag) {
                ValidateCodeUtil.writeCodeToEmail(mail, user.getName(), session);
                status = "1"; // 发送成功
            } else {
                status = "-1"; // 该邮箱已经被注册
            }
        } else {
//            String code1 = (String) session.getAttribute(user.getMail());
//            if (FormValidateUtil.isCode(code, code1, true)) {
                boolean flag = accountCentralService.queryishaveUserByMail(mail);
                if (!flag) {
                    ValidateCodeUtil.writeCodeToEmail(mail, user.getName(), session);
                    status = "1"; // 发送成功
                } else {
                    status = "-1"; // 该邮箱已经被注册
                }
//            } else {
//                status = "-2"; //异常操作
//            }
        }
        switch (status) {
            case "1":
                logger.info("用户" + user.getDevId() + " 邮箱" + mail + " 获取验证码");
                break;
            case "-1":
                logger.error("用户" + user.getDevId() + " 邮箱" + mail + " 获取验证码 失败");
                break;
            case "-2":
                logger.error("用户" + user.getDevId() + " 邮箱" + mail + " 获取验证码 失败");
                break;
        }
        return status;
    }

    // ---------------------------好友管理模块------------------------------------

    /**
     * 我的好友 展示
     *
     * @param session
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxfriendsManager")
    public Map<String, Object> friendsManager(HttpSession session, String page) throws Exception {
        String devId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intPage = Integer.parseInt(page);
        return accountCentralService.friendListByID(devId, intPage);
    }

    /**
     * 按条件查询我的朋友列表
     *
     * @param session
     * @param page
     * @param condition
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxfindmyfriend")
    public Map<String, Object> findmyfriend(HttpSession session, String page, String condition) throws Exception {
        String devId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        Map<String, Object> map = accountCentralService.findMyfriendByCondition(devId, intpage, condition);
        return map;
    }

    /**
     * 按首字母查询 我的 朋友列表
     *
     * @param session
     * @param first
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxFriendsByFirstLetter")
    public Map<String, Object> selectFriendsByFirstLetter(HttpSession session, String first, String page) throws Exception {
        String devId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        Map<String, Object> map = accountCentralService.selectFriendsByFirstLetter(devId, intpage, first);
        return map;
    }

    /**
     * 加载单个朋友的详细信息
     *
     * @param friendId 暂时指定为 friendId 好友的用户id
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxselectFriendByFid")
    public FriendBean selectFriendByFid(Integer friendId) throws Exception {
        Map<String, Object> map = accountCentralService.selectFriendByFid(friendId);
        return (FriendBean) map.get("friend");
    }

    /**
     * 验证支付密码
     *
     * @param session
     * @param payPsw
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxValidatePayPsw")
    public String validatePayPsw(HttpSession session, String payPsw) throws Exception {
        String userId = session.getAttribute("id").toString();
        boolean flag = adScoreManagerService.validatePayPsw(userId, payPsw);
        if (flag) {
            return "1";
        } else {
            return "-1"; // 1 -1 支付密码验证成功 失败
        }
    }

    /**
     * 校验支付密码成功后 向好友转账
     *
     * @param session
     * @param friendId
     * @param outBalance
     * @param note
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxtransferToFriend")
    public String transferToFriend(HttpSession session, String friendId, String outBalance, String note)
            throws Exception {
        String userId = session.getAttribute("id").toString();
        int flag = adScoreManagerService.payToFriendExecute(userId, friendId, outBalance, note);
        if (flag == 1) {
            userMsgService.sendMsgForAccountDefray(Integer.valueOf(userId), Double.valueOf(outBalance),
                    ScoreHelper.getDealType(ScoreHelper.transferToFriend));
            return "1"; // 向好友支付 成功
        }
        return "-1"; // 向好友支付 失败 1 -1
    }

    /**
     * 删除好友
     *
     * @param session
     * @param deleteId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxdeletefriend")
    public String deletefriend(HttpSession session, String deleteId) throws Exception {
        String userId = session.getAttribute("id").toString();
        return accountCentralService.deletefriend(userId, deleteId); // 0:服务器异常 1：删除成功 -1:删除失败
    }

    /**
     * 发送添加好友消息
     */
    @ResponseBody
    @RequestMapping("/ajaxaddfriend")
    public Integer ajaxaddfriend(HttpSession session, String friendId, String news) throws Exception {
        Integer devId = (Integer) session.getAttribute("id");
        return userMsgService.sendMsgForAddFriend(devId, Integer.parseInt(friendId), news); // 0:服务器异常 1：发送成功 -1:发送失败
    }

    /**
     * 好友同意申请之后 执行添加好友的方法
     *
     * @param session
     * @param friendId 对方id（发起人id）
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxaddFriendExecute")
    public String addFriend(HttpSession session, String friendId, Integer msgId) throws Exception {
        Integer id = (Integer) session.getAttribute("id");
        String status = accountCentralService.addFriendExecute(friendId, id + "");
        switch (status) {
            case "1": // 通知对方已经添加好友成功
                logger.info("用户" + id + " 同意 添加好友" + friendId);
                try {
                    userMsgService.sendMsgForAddFriendRes(Integer.valueOf(friendId), id, true, msgId);
                } catch (Exception e) {
                    status = "0";
                }
                break;
            case "-1": // -1:添加失败
                logger.error("用户" + id + " 同意 添加好友" + friendId + " 失败");
                break;
            default: // 0:服务器异常 -1:添加失败 1:添加好友成功 -2:添加成功的消息发送失败
                break;
        }
        return status;
    }

    /**
     * 好友拒绝申请之后 执行拒绝添加好友的方法
     *
     * @param session
     * @param friendId 对方id（发起人id）
     * @param msgId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxaddFriendUnExecute")
    public Integer addFriendUn(HttpSession session, String friendId, Integer msgId) throws Exception {
        Integer id = (Integer) session.getAttribute("id");
        String status = accountCentralService.addFriendUnExecute(friendId, id + "");
        if (status.equals("1")) {
            logger.info("用户" + id + " 拒绝 添加好友" + friendId);
            userMsgService.sendMsgForAddFriendRes(Integer.valueOf(friendId), id, false, msgId);
            return 1;
        }
        return -1; // 0:服务器异常 -1:拒绝失败 1:拒绝好友成功 -2:添加失败的消息发送失败
    }

    // ---------------------------我的银行卡------------------------------------

    /**
     * 我的银行卡页，加载出持有的银行卡数量和详情
     */
    @ResponseBody
    @RequestMapping("/ajaxbankInfo")
    public List<UserAccountVo> bankInfo(HttpSession session) throws Exception {
        String divId = session.getAttribute("id").toString();
        return accountCentralService.queryBankCardsById(divId);
    }

    /**
     * 新增银行卡 由银行向银行卡绑定的手机号码发送校验码
     *
     * @param session
     * @param accountNum 银行卡号
     * @param tel_num    手机号码
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("ajaxGetCodeToBankCardPhone")
    public String getCodeToBankCardPhone(HttpSession session, String accountNum, String tel_num) throws Exception {
        String flag;
        // 校验是否绑定过此银行卡
        String userId = session.getAttribute("id").toString();
        Boolean status = accountCentralService.getCardStatus(accountNum, userId);
        if (!status) {
            // 由银行向银行卡绑定的手机号码发送校验码

            //发送成功之后
            flag = "1";
            logger.info("用户" + userId + " 新增银行卡 " + accountNum + "发送验证码到手机号" + tel_num);
            // 存入银行发送的校验码
            session.setAttribute(tel_num, "666666");
            session.setAttribute("bankTel", tel_num);
        } else {
            flag = "-2";
            logger.error("用户" + userId + " 新增银行卡 " + accountNum + "发送验证码到手机号" + tel_num + " 失败");
        }

        return flag; // 1:发送成功 -1:发送失败 0:服务器异常 -2银行卡已占用
    }

    /**
     * 同意协议并绑定 校验 验证码
     *
     * @param code
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxValidateBankCode")
    public String validateBankCode(HttpSession session, String code) throws Exception {
        // 同意协议并绑定 校验 验证码
        String status;
        String bankTel = session.getAttribute("bankTel").toString();
        String code1 = session.getAttribute(bankTel).toString();
        if (FormValidateUtil.isCode(code, code1, true)) {
            status = "1";
        } else {
            status = "-1";
        }
        return status; // 1 -1
    }

    /**
     * 新增银行卡 第 3 步
     *
     * @param userAccount
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxaddNewBankCard_StepThree")
    public String addNewBankCard_StepThree(HttpSession session, UserAccountBean userAccount) throws Exception {
        String id = session.getAttribute("id").toString();
        userAccount.setUserId(Long.parseLong(id));
        int flag = accountCentralService.addNewBankCard(userAccount);
        if (flag == 1) {
            return "1";
        } else {
            return "-1"; // -1:绑定失败 1:绑定成功
        }
    }

    /**
     * 查看一张银行卡的详细信息
     *
     * @param session
     * @param accountId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxqueryBankCardOne")
    public UserAccountVo queryBankCardOne(HttpSession session, String accountId) throws Exception {
        String divId = session.getAttribute("id").toString();
        UserAccountVo userAccountVo = accountCentralService.queryBankCardOne(divId, accountId);
        if (userAccountVo != null && userAccountVo.getUserAccount() != null) {
            if (userAccountVo.getUserAccount().getBankName() != null) {
                return userAccountVo;
            }
        }
        return null;
    }

    /**
     * 修改平台预留电话 获取旧手机验证码 修改支付密码时候获取的验证码
     */
    @ResponseBody
    @RequestMapping("/ajaxgetCodeToOldPhone")
    public String getCodeToOldPhone(HttpSession session) throws Exception {
        UserLoginBean user = (UserLoginBean) session.getAttribute("user");
        String flag;
        try {
            // 发送验证码到手机
            ValidateCodeUtil.writeCodeToMobile(user.getTel_num(), session);
            flag = "1";
            logger.info("用户" + user.getDevId() + " 修改平台预留手机号码 发送验证码到 旧手机号" + user.getTel_num());
        } catch (Exception e) {
            flag = "-1";
        }
        return flag; // 0 1 -1
    }

    /**
     * 验证 旧 手机验证码是否正确 修改支付密码时候的验证方法
     *
     * @param session
     * @param code
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxValidateCodeOldPhone")
    public String ValidateCodeOldPhone(HttpSession session, String code) throws Exception {
        String flag;
        UserLoginBean user = (UserLoginBean) session.getAttribute("user");
        String code1 = (String) session.getAttribute(user.getTel_num());
        if (FormValidateUtil.isCode(code1, code, true)) {
            flag = "1";
            logger.info("用户" + user.getDevId() + " 旧手机" + user.getTel_num() + " 验证码" + code + " 校验");
//            session.setAttribute(user.getTel_num(), null);
        } else {
            flag = "-1";
        }
        return flag; // 0服务器异常 1校验成功 -1验证码错误
    }

    /**
     * 新手机获取验证码
     *
     * @param tel_num
     * @param session
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxGetCodeToNewPhone")
    public String getCodeToNewPhone(String tel_num, String code, HttpSession session) throws Exception {
        UserLoginBean user = (UserLoginBean) session.getAttribute("user");
//        String code1 = (String) session.getAttribute(user.getTel_num());
//        if (FormValidateUtil.isCode(code1, code, true)) {
        try {
            Integer integer = loginService.hasPhone(tel_num);
            if (integer != null) {
                return "-1";
            }
            ValidateCodeUtil.writeCodeToMobile(tel_num, session);
            logger.info("用户" + user.getDevId() + " 修改平台预留手机号码 发送验证码到 新手机号" + tel_num);
            return "1";
        } catch (Exception e) {
            logger.error("用户" + user.getDevId() + " 修改平台预留手机号码 发送验证码到 新手机号" + tel_num + " 失败", e);
        }
//        } else
//            return "-2";
        return "0"; // 0 1 -1 发送成功失败 -2返回上一页 验证码过期或异常操作
    }

    /**
     * 验证新手机号的验证码是否正确
     *
     * @param session
     * @param tel_num
     * @param code
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxValidateCodeNewPhone")
    public String ValidateCodeNewPhone(HttpSession session, String tel_num, String code) throws Exception {
        String flag;
        String code1 = (String) session.getAttribute(tel_num);
        if (FormValidateUtil.isCode(code1, code, true)) {
            flag = "1";
            logger.info("用户" + session.getAttribute("id") + " 新手机" + tel_num + " 验证码" + code + " 校验");
            session.setAttribute(tel_num, null);
        } else {
            flag = "-1";
        }
        return flag; // 0 1 -1 校验成功失败
    }

    /**
     * 更新平台预留手机号
     *
     * @param session
     * @param tel_num
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxSaveNewPhone")
    public String saveNewPhone(HttpSession session, String tel_num) throws Exception {
        String id = session.getAttribute("id").toString();
        int flag = accountCentralService.saveNewPhone(id, tel_num);
        if (flag > 0) {
            UserLoginBean user = (UserLoginBean) session.getAttribute("user");
            user.setTel_num(tel_num);
            session.setAttribute("user", user);
            logger.info("更新用户" + id + " 平台预留手机号为" + tel_num);
            return "1";
        } else {
            return "-1"; // 1 -1 手机号码更新成功失败
        }
    }

    /**
     * 解除与已有银行卡的绑定
     *
     * @param session
     * @param accountId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxcancelBanding")
    public String cancelBanding(HttpSession session, String accountId) throws Exception {
        String id = session.getAttribute("id").toString();
        int flag = accountCentralService.cancelBandingCard(id, accountId); // 测试后修改
        if (flag > 0) {
            logger.info("用户" + id + " 解除与已有银行卡（账户id）" + accountId + " 的绑定");
            return "1";
        } else {
            return "-1"; // 1 -1 解除银行卡绑定是否成功
        }
    }
    // ---------------------------账号安全------------------------------------

    @ResponseBody
    @RequestMapping("/ajaxGetPayPswStatus")
    public boolean getPayPswStatus(HttpSession session) throws Exception {
        String userId = session.getAttribute("id").toString();
        return accountCentralService.getPayPswStatus(userId);
    }

    /**
     * 获取旧mail邮箱验证码
     */
    @ResponseBody
    @RequestMapping("/ajaxGetCodeToOldMail")
    public String getCodeToOldMail(HttpSession session) throws Exception {
        UserLoginBean user = (UserLoginBean) session.getAttribute("user");
        String flag;
        try {
            ValidateCodeUtil.writeCodeToEmail(user.getMail(), user.getName(), session);
            flag = "1";
            logger.info("用户" + user.getDevId() + " 修改邮箱 发送验证码到 邮箱" + user.getMail());
        } catch (Exception e) {
            flag = "-1";
            logger.error("用户" + user.getDevId() + " 修改平台预留手机号码 发送验证码到 邮箱" + user.getMail(), e);
        }
        return flag; // 0 1 -1 验证码是否发送成功
    }

    /**
     * 验证 旧 邮箱验证码是否正确
     *
     * @param session
     * @param code
     * @return
     * @throws Exception </pre>
     */
    @RequestMapping("/ajaxValidateCodeOldMail")
    @ResponseBody
    public String ValidateCodeOldMail(HttpSession session, String code) throws Exception {
        UserLoginBean user = (UserLoginBean) session.getAttribute("user");
        String flag = "-1";
        String code1 = (String) session.getAttribute(user.getMail());
        if (FormValidateUtil.isCode(code1, code, true)) {
            flag = "1";
            session.setAttribute(user.getMail(), null);
            logger.info("用户" + session.getAttribute("id") + " 旧邮箱" + user.getMail() + " 验证码" + code + " 校验");
        }
        return flag; // 0 1 -1 旧邮箱验证码是否正确
    }

    /**
     * 新邮箱获取验证码
     *
     * @param mail
     * @param session
     * @return
     * @throws Exception </pre>
     */
//    @ResponseBody
//    @RequestMapping("/ajaxgetCodeToNewMail")
//    public String getCodeToNewMail(String mail, HttpSession session) throws Exception {
//        String flag = "0";
//        try {
//            boolean flag2 = accountCentralService.queryishaveUserByMail(mail);
//            if (!flag2) {
//                ValidateCodeUtil.writeCodeToMobile(mail, session);
//                flag = "1";
//            }
//        } catch (Exception e) {
//            flag = "0";
//        }
//        return flag; // 0 1 -1 验证码是否发送成功
//    }

    /**
     * 验证新邮箱的验证码是否正确
     *
     * @param session
     * @param mail
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxValidateCodeNewMail")
    public String ValidateCodeNewMail(HttpSession session, String mail, String code) throws Exception {
        String flag;
        String code1 = (String) session.getAttribute(mail);
        if (FormValidateUtil.isCode(code1, code, true)) {
            flag = "1";
            session.setAttribute(mail, null);
            logger.info("用户" + session.getAttribute("id") + " 新邮箱" + mail + " 验证码" + code + " 校验");
        } else {
            flag = "-1";
        }
        return flag; // 0 1 -1 新邮箱验证码是否正确
    }

    /**
     * 更新邮箱
     *
     * @param session
     * @param mail
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxSaveNewMail")
    public String saveNewMail(HttpSession session, String mail) throws Exception {
        String id = session.getAttribute("id").toString();
        String status = accountCentralService.saveNewMail(id, mail);
        if (status.equals("1")) {
            UserLoginBean user = (UserLoginBean) session.getAttribute("user");
            user.setMail(mail);
            session.setAttribute("user", user);
            logger.info("更新用户" + session.getAttribute("id") + " 绑定邮箱为" + mail);
        } else {
            status = "-1";
            logger.error("更新用户" + session.getAttribute("id") + " 绑定邮箱为" + mail + " 失败");
        }
        return status; // 0 1 -1 更新邮箱是否成功
    }

    /**
     * 验证输入的原密码是否正确
     */
    @ResponseBody
    @RequestMapping("/ajaxValidateOldPsw")
    public String validateOldPsw(HttpSession session, String oldPsw) throws Exception {
        String userId = session.getAttribute("id").toString();
        String flag;
        int status = accountCentralService.ensureOldPsw(userId, MD5.encode(oldPsw));
        if (status > 0) {
            flag = "1";
        } else {
            flag = "-1";
        }
        logger.info("用户" + userId + " 修改登录密码 校验旧密码");
        return flag; // 0 1 -1 输入的原密码是否正确
    }

    /**
     * 修改 登陆 密码
     *
     * @param session
     * @param newPsw
     * @return
     * @throws Exception </pre>
     */
    @ResponseBody
    @RequestMapping("/ajaxUpdatePsw")
    public String updatePsw(HttpSession session, String newPsw) throws Exception {
        String devId = session.getAttribute("id").toString();
        String status = accountCentralService.updatePsw(devId, newPsw);
        return status; // 0 1 -1 更新登陆密码是否成功
    }

    /**
     * 验证输入的原支付密码是否正确
     */
    @ResponseBody
    @RequestMapping("/ajaxValidateOldPayPsw")
    public String validateOldPayPsw(HttpSession session, String oldPayPsw) throws Exception {
        String userId = session.getAttribute("id").toString();
        String flag;
        int status = accountCentralService.ensureOldPayPsw(userId, MD5.encode(oldPayPsw));

        if (status > 0) {
            flag = "1";
        } else {
            flag = "-1";
        }
        return flag; // 0 1 -1 输入的原支付密码是否正确
    }

    /**
     * 修改 支付 密码
     *
     * @param session
     * @param newPayPsw
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/ajaxUpdatePayPsw")
    public String updatePayPsw(HttpSession session, String newPayPsw) throws Exception {
        String devId = session.getAttribute("id").toString();
        String status = accountCentralService.resetPayPsw(devId, newPayPsw);
        return status; // 0 1 -1 更新支付密码是否成功 -2 支付密码和登录密码相同
    }

    /**
     * 重置支付密码
     *
     * @param session
     * @param newPayPsw
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxResetPayPsw")
    public String resetPayPsw(HttpSession session, String newPayPsw) throws Exception {
        String devId = session.getAttribute("id").toString();
        String status = accountCentralService.resetPayPsw(devId, newPayPsw);
        return status; // 0 1 -1 重置支付密码是否成功 -2 支付密码和登录密码相同
    }

    /**
     * 查询和更新用户支付密码状态（redis）
     *
     * @param session
     * @param status  密码是否输入正确  true / false  为空时获取用户支付密码状态（是否锁定）
     * @return 解锁的时候传入status=true  返回-2  异常返回-1
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxGetPayPswTimes")
    public String ajaxGetPayPswTimes(HttpSession session, Boolean status) throws Exception {
        String userId = session.getAttribute("id").toString();
        return accountCentralService.getPayPswTimes(userId, status);
    }

}